package com.binhai.springboot.user.service;


import com.binhai.springboot.person.bean.PersonBean;
import com.binhai.springboot.person.dao.PersonBeanDao;
import com.binhai.springboot.user.bean.UserBean;
import com.binhai.springboot.user.dao.UserBeanDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {


    @Autowired
    private UserBeanDao beanDao;

    @Autowired
    private HttpSession session;

    @Autowired
    private PersonBeanDao postDao;

    public static int UserId;

    public List<UserBean> teacherinfor() {
        return beanDao.teacherinfor ( );
    }

    public List getAllData() {

        return beanDao.getAllData ( );
    }

    public List selectData(String name) {

        return beanDao.selectData (name);

    }

    public Map saveData(UserBean bean) {
        Map re = new HashMap ( );
        try {
            beanDao.insertReturnId (bean);
            beanDao.insertOrgids (bean);
            beanDao.insertRoleids (bean);
            beanDao.insertPostids (bean);
            beanDao.savePerson (bean);
            re.put ("result", "success");
        } catch (Exception e) {
            re.put ("result", "error");
            e.printStackTrace ( );
        }
        return re;
    }


    public Map updateData(UserBean bean) {
        Map re = new HashMap ( );
        try {
            //修改数据
            beanDao.updateByPrimaryKeySelective (bean);
            //删除关联的部门信息
            beanDao.deleteOrgids (bean);
            //批量保存关联的部门信息
            beanDao.insertOrgids (bean);
            //批量保存关联的角色信息
            beanDao.insertRoleids (bean);
            beanDao.insertPostids (bean);

            re.put ("result", "success");
        } catch (Exception e) {
            re.put ("result", "error");
            e.printStackTrace ( );
        }
        return re;
    }

    public Map deleteData(UserBean bean) {
        Map re = new HashMap ( );
        try {

            beanDao.deleteData (bean);
            beanDao.deleteOrgids (bean);
            re.put ("result", "success");
        } catch (Exception e) {
            re.put ("result", "error");
            e.printStackTrace ( );
        }
        return re;
    }

    public Map deleteDatas(int[] ids) {
        Map re = new HashMap ( );
        try {
            //批量删除用户数据
            beanDao.deleteDatas (ids);
            //批量删除关联的部门信息
            beanDao.deleteOrgidsForBath (ids);
            re.put ("result", "success");
        } catch (Exception e) {
            re.put ("result", "error");
            e.printStackTrace ( );
        }
        return re;
    }

    /**
     * 登录
     *
     * @param bean
     * @return
     */
    public String login(UserBean bean,String code,PersonBean beanbean) {
        String sessionCode = (String) session.getAttribute ("code");

        UserBean user = beanDao.login (bean);
        PersonBean person = postDao.person (beanbean);

        UserId = beanDao.selectUP (bean);
        System.out.println (UserId);
        if (user != null && sessionCode.equalsIgnoreCase (code)) {
//            登录成功 记录用户信息
            session.setAttribute ("user", user);
            session.setAttribute ("person", person);
            return "index";
        } else {
            return "login";
        }
    }

    public List getAllTreeData() {
        return beanDao.getAllTreeData ( );
    }

    public String register(UserBean bean) {
        beanDao.register (bean);
        beanDao.registerkz (bean);
        return "login.do";
    }

    public List forget(String beizhu) {
        return beanDao.forget (beizhu);
    }

    public String repass(UserBean bean) {
        bean.setId (UserId);
        System.out.println (bean.getRepass ());

        beanDao.repass (bean);
        return "login";
    }
}